package com.techsoft;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class SQLObject extends HashMap<String, Object> {
	private static final long serialVersionUID = 7438804026384736667L;
	private transient List<SQLParam> selectParams;
	private transient List<MetaData> metaDatas;
	private transient List<SQLParam> insertParams;
	private transient List<SQLParam> updateParams;
	private transient List<SQLParam> deleteParams;
	private transient List<SQLParam> singleSelectParams;

	public SQLObject() {
		selectParams = new ArrayList<SQLParam>();
		insertParams = new ArrayList<SQLParam>();
		updateParams = new ArrayList<SQLParam>();
		deleteParams = new ArrayList<SQLParam>();
		metaDatas = new ArrayList<MetaData>();
		singleSelectParams = new ArrayList<SQLParam>();
	}

	private void buildSelectParams() {
		this.selectParams.clear();
		String tempdata = this.getDefselectparams();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			SQLParam param = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				param = new SQLParam();
				param.setDtype(obj.getString("DTYPE"));
				param.setDtypename(obj.getString("DTYPENAME"));
				param.setIotype(obj.getString("IOTYPE"));
				param.setIndex(obj.getString("INDEX"));
				param.setName(obj.getString("NAME"));
				this.selectParams.add(param);
			}
		}
	}

	private void buildInsertParams() {
		this.insertParams.clear();
		String tempdata = this.getDefinsertparams();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			SQLParam param = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				param = new SQLParam();
				param.setDtype(obj.getString("DTYPE"));
				param.setDtypename(obj.getString("DTYPENAME"));
				param.setIotype(obj.getString("IOTYPE"));
				param.setIndex(obj.getString("INDEX"));
				param.setName(obj.getString("NAME"));
				this.insertParams.add(param);
			}
		}
	}

	private void buildUpdateParams() {
		this.updateParams.clear();
		String tempdata = this.getDefupdateparams();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			SQLParam param = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				param = new SQLParam();
				param.setDtype(obj.getString("DTYPE"));
				param.setDtypename(obj.getString("DTYPENAME"));
				param.setIotype(obj.getString("IOTYPE"));
				param.setIndex(obj.getString("INDEX"));
				param.setName(obj.getString("NAME"));
				this.updateParams.add(param);
			}
		}
	}

	private void buildDeleteParams() {
		this.deleteParams.clear();
		String tempdata = this.getDefdeleteparams();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			SQLParam param = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				param = new SQLParam();
				param.setDtype(obj.getString("DTYPE"));
				param.setDtypename(obj.getString("DTYPENAME"));
				param.setIotype(obj.getString("IOTYPE"));
				param.setIndex(obj.getString("INDEX"));
				param.setName(obj.getString("NAME"));
				this.deleteParams.add(param);
			}
		}
	}
	
	private void buildSingleSelectParams(){
		this.singleSelectParams.clear();
		String tempdata = this.getDefSingleSelectParams();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			SQLParam param = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				param = new SQLParam();
				param.setDtype(obj.getString("DTYPE"));
				param.setDtypename(obj.getString("DTYPENAME"));
				param.setIotype(obj.getString("IOTYPE"));
				param.setIndex(obj.getString("INDEX"));
				param.setName(obj.getString("NAME"));
				this.singleSelectParams.add(param);
			}
		}
	}

	private void buildMetaDatas() {
		this.metaDatas.clear();
		String tempdata = this.getDefselectmetadata();
		if ((tempdata != null) && (!tempdata.equals(""))) {
			MetaData metadata = null;
			JSONObject obj = null;
			JSONArray datas = JSON.parseArray(tempdata);
			Iterator<Object> iter = datas.iterator();
			while (iter.hasNext()) {
				obj = (JSONObject) iter.next();
				metadata = new MetaData();
				metadata.setDtypename(obj.getString("DTYPENAME"));
				metadata.setPrecision(obj.getInteger("PRECISION"));
				metadata.setScale(obj.getInteger("SCALE"));
				metadata.setFname(obj.getString("FNAME"));
				metadata.setNullable(obj.getString("NULLABLE"));
				metadata.setMaxlen(obj.getInteger("MAXLEN"));
				metadata.setDtype(obj.getInteger("DTYPE"));
				this.metaDatas.add(metadata);
			}
		}
	}

	public String getOid() {
		Object tempdata = this.get("oid");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setOid(String oid) {
		this.put("oid", oid);
	}

	public String getPid() {
		Object tempdata = this.get("pid");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setPid(String pid) {
		this.put("pid", pid);
	}

	public String getMid() {
		Object tempdata = this.get("mid");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setMid(String mid) {
		this.put("mid", mid);
	}

	public String getSqlId() {
		Object tempdata = this.get("sqlid");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setSqlId(String sqlId) {
		this.put("sqlid", sqlId);
	}

	public Long getSqlOptions() {
		Object tempdata = this.get("sqloptions");
		if (tempdata != null) {
			return (Long) tempdata;
		}

		return 0L;
	}

	public void setSqlOptions(Long sqlOptions) {
		this.put("sqloptions", sqlOptions);
	}

	public String getSqlName() {
		Object tempdata = this.get("sqlname");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setSqlName(String sqlName) {
		this.put("sqlname", sqlName);
	}

	public String getTableName() {
		Object tempdata = this.get("tablename");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setTableName(String tableName) {
		this.put("tablename", tableName);
	}

	public String getSqlDesc() {
		Object tempdata = this.get("sqldesc");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setSqlDesc(String sqlDesc) {
		this.put("sqldesc", sqlDesc);
	}

	public String getDefSelectSql() {
		Object tempdata = this.get("defselectsql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefSelectSql(String defSelectSql) {
		this.put("defselectsql", defSelectSql);
	}

	public String getDefselectparams() {
		Object tempdata = this.get("defselectparams");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefselectparams(String defselectparams) {
		this.put("defselectparams", defselectparams);
		this.buildSelectParams();
	}

	public String getDefselectmetadata() {
		Object tempdata = this.get("defselectmetadata");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefselectmetadata(String defselectmetadata) {
		this.put("defselectmetadata", defselectmetadata);
		this.buildMetaDatas();
	}

	public String getDefInsertSql() {
		Object tempdata = this.get("definsertsql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefInsertSql(String defInsertSql) {
		this.put("definsertsql", defInsertSql);
	}

	public String getDefinsertparams() {
		Object tempdata = this.get("definsertparams");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefinsertparams(String definsertparams) {
		this.put("definsertparams", definsertparams);
		this.buildInsertParams();
	}

	public String getDefUpdateSql() {
		Object tempdata = this.get("defupdatesql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefUpdateSql(String defUpdateSql) {
		this.put("defupdatesql", defUpdateSql);
	}

	public String getDefupdateparams() {
		Object tempdata = this.get("defupdateparams");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefupdateparams(String defupdateparams) {
		this.put("defupdateparams", defupdateparams);
		this.buildUpdateParams();
	}

	public String getDefDeleteSql() {
		Object tempdata = this.get("defdeletesql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefDeleteSql(String defDeleteSql) {
		this.put("defdeletesql", defDeleteSql);
	}

	public String getDefdeleteparams() {
		Object tempdata = this.get("defdeleteparams");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefdeleteparams(String defdeleteparams) {
		this.put("defdeleteparams", defdeleteparams);
		this.buildDeleteParams();
	}

	public String getSelectSql() {
		Object tempdata = this.get("selectsql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setSelectSql(String selectSql) {
		this.put("selectsql", selectSql);
	}

	public String getInsertSql() {
		Object tempdata = this.get("insertsql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setInsertSql(String insertSql) {
		this.put("insertsql", insertSql);
	}

	public String getUpdateSql() {
		Object tempdata = this.get("updatesql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setUpdateSql(String updateSql) {
		this.put("updatesql", updateSql);
	}

	public String getDeleteSql() {
		Object tempdata = this.get("deletesql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDeleteSql(String deleteSql) {
		this.put("deletesql", deleteSql);
	}

	public Date getUpdateDate() {
		Object tempdata = this.get("updatedate");
		if (tempdata != null) {
			return (Date) tempdata;
		}

		return null;
	}

	public void setUpdateDate(Date updateDate) {
		this.put("updatedate", updateDate);
	}

	public Long getObjectVersion() {
		Object tempdata = this.get("objectversion");
		if (tempdata != null) {
			return (Long) tempdata;
		}

		return 0L;
	}

	public void setObjectVersion(Long objectVersion) {
		this.put("objectversion", objectVersion);
	}

	public List<SQLParam> getSelectParams() {
		return selectParams;
	}

	public List<MetaData> getMetaDatas() {
		return metaDatas;
	}

	public List<SQLParam> getInsertParams() {
		return insertParams;
	}

	public List<SQLParam> getUpdateParams() {
		return updateParams;
	}

	public List<SQLParam> getDeleteParams() {
		return deleteParams;
	}

	// 是否返回游标
	public boolean getIsCursor() {
		boolean result = false;
		Long sqloptions = this.getSqlOptions();
		if (sqloptions != null) {
			result = ((sqloptions & 1) == 1);
		}

		return result;
	}

	// 是否整体缓存
	public boolean getIsSQLIDCache() {
		boolean result = false;
		Long sqloptions = this.getSqlOptions();
		if (sqloptions != null) {
			result = ((sqloptions & 2) == 2);
		}

		return result;
	}

	// 是否单条记录缓存
	public boolean getIsKeyCache() {
		boolean result = false;
		Long sqloptions = this.getSqlOptions();
		if (sqloptions != null) {
			result = ((sqloptions & 4) == 4);
		}

		return result;
	}

	public String getKeyField() {
		Object tempdata = this.get("keyfield");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setKeyField(String keyField) {
		this.put("keyfield", keyField);
	}
	
	public String getDefSingleSelectSql() {
		Object tempdata = this.get("def_singleselect_sql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setDefSingleSelectSql(String defkeycachesql) {
		this.put("def_singleselect_sql", defkeycachesql);
	}
	public String getSingleSelectSql() {
		Object tempdata = this.get("singleselect_sql");
		if (tempdata != null) {
			return (String) tempdata;
		}

		return null;
	}

	public void setSingleSelectSql(String keycachesql) {
		this.put("singleselect_sql", keycachesql);
	}
	
	public String getDefSingleSelectParams() {
		Object tempdata = this.get("def_singleselect_params");
		if (tempdata != null) {
			return (String) tempdata;
		}
		return null;
	}

	public void setDefSingleSelectParams(String defkeycacheparams) {
		this.put("def_singleselect_params", defkeycacheparams);
		this.buildSingleSelectParams();
	}
	
}
